﻿function AzureOnlineTableDeleteView(context) {

    var self = this;

    var dispatcher = $({});

    var spinner = new SpinnerModel();

    var table = ko.observable(null);

    function remove() {
        var tbl = table();
        var name = tbl.name();
        var api = context.api();
        spinner.enter();
        api.tables.removeTable({
            table: name,
            success: function () {
                spinner.leave();
                context.tables.removeTable(tbl);
                dispatcher.trigger('success');
            },
            error: function () {
                spinner.leave();
                return 'notify';
            }
        });
    }

    function addOnSuccess(handler) {
        dispatcher.bind('success', handler);
    };

    function init(tbl) {
        table(tbl);
    }

    function initFromCurrent() {
        var name = context.tables.table();
        var tbl = context.tables.findByName(name);
        table(tbl);
    }

    self.spinner = spinner;
    self.table = table;
    self.init = init;
    self.initFromCurrent = initFromCurrent;
    self.remove = remove;
    self.addOnSuccess = addOnSuccess;
}